const App = getApp();

// 富文本插件
import wxParse from '../../../wxParse/wxParse.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareIcon: '', //分享的封面图
    // 文章详情
    detail: {},
    toupiao:0,
    trueMobile:'',
    shareTitle:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var _toupiao = options.toupiao>0 ? options.toupiao : 0;
    this.setData({toupiao:_toupiao});
    // 获取文章详情
    this.getArticleDetail(options.article_id);
    // 设置navbar标题、颜色
    App.setNavigationBar();
  },
  onShow(){
    this.setData({trueMobile:wx.getStorageSync('true_mobile')});
  },
  /**
   * 获取文章详情
   */
  getArticleDetail(article_id) {
    let _this = this;
    App._get('article/detail', {
      article_id
    }, function (result) {
      let detail = result.data.detail;

      console.log('获取到了文章详情：', detail);
      // 富文本转码
      if (detail.article_content.length > 0) {
        wxParse.wxParse('content', 'html', detail.article_content, _this, 0);
      }
      var theShareTitle = _this.data.toupiao==1 ? '快来看看【'+detail.article_title+'】的作品吧，给Ta投上一票' : detail.article_title;
      _this.setData({
        detail,
        shareIcon:detail.image.file_path,
        shareTitle:theShareTitle
      });
    });
  },
  //获取电话号码之后
  getUserMobile22(e){
    const _this = this;
    var wzid = e.currentTarget.dataset.wzid
    App.getUserMobile(e, function(mobile, myPara){
      _this.setData({trueMobile: mobile});
      _this.toupiaoFun(myPara);
    }, wzid);
  },
  //确定开始投票函数
  toupiaoClick(e){
    var wzid = e.currentTarget.dataset.wzid
    this.toupiaoFun(wzid);
  },
  toupiaoFun(wzid){
    //App.showError('开始投票'+wzid);
    const _this = this;
    var _oldObj = this.data.detail;
    App._get('article/addtoupiao', {tpartid:wzid, userid:wx.getStorageSync('user_id')}, function(result) {
      let _back = result.data.back;
      if(_back.isok=='no'){
        App.showError(_back.msg);
      }
      else{
        _oldObj['get_piaonum'] = _back['piaoNum'];
        _this.setData({detail:_oldObj});
        App.showError('投票成功');
      }
    });
  },
  /** 分享当前页面 */
  onShareAppMessage() {
    // 构建页面参数
    const params = App.getShareUrlParams({ 'article_id': this.data.detail.article_id, 'toupiao':this.data.toupiao });
    return {
      imageUrl: this.data.shareIcon,
      title: this.data.shareTitle,
      path: "/pages/article/detail/index?" + params
    };
  },
  /** 分享到朋友圈 */
  onShareTimeline() {
    // 构建页面参数
    const params = App.getShareUrlParams({ 'article_id': this.data.detail.article_id, 'toupiao':this.data.toupiao });
    return {
      title: this.data.shareTitle,
      path: "/pages/article/detail/index?" + params
    };
  }

})